home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Modules / md5module.c < prev    next >
C/C++ Source or Header  |  1998-01-25  |  5KB  |  208 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* MD5 module */
  33.  
  34. /* This module provides an interface to the RSA Data Security,
  35.    Inc. MD5 Message-Digest Algorithm, described in RFC 1321.
  36.    It requires the files md5c.c and md5.h (which are slightly changed
  37.    from the versions in the RFC to avoid the "global.h" file.) */
  38.  
  39.  
  40. /* MD5 objects */
  41.  
  42. #include "Python.h"
  43. #include "md5.h"
  44.  
  45. typedef struct {
  46.     PyObject_HEAD
  47.         MD5_CTX    md5;        /* the context holder */
  48. } md5object;
  49.  
  50. staticforward PyTypeObject MD5type;
  51.  
  52. #define is_md5object(v)        ((v)->ob_type == &MD5type)
  53.  
  54. #include "protos/md5module_protos.h"
  55.  
  56. static md5object *
  57. newmd5object()
  58. {
  59.     md5object *md5p;
  60.  
  61.     md5p = PyObject_NEW(md5object, &MD5type);
  62.     if (md5p == NULL)
  63.         return NULL;
  64.  
  65.     MD5Init(&md5p->md5);    /* actual initialisation */
  66.     return md5p;
  67. }
  68.  
  69.  
  70. /* MD5 methods */
  71.  
  72. static void
  73. md5_dealloc(md5p)
  74.     md5object *md5p;
  75. {
  76.     PyMem_DEL(md5p);
  77. }
  78.  
  79.  
  80. /* MD5 methods-as-attributes */
  81.  
  82. static PyObject *
  83. md5_update(self, args)
  84.     md5object *self;
  85.     PyObject *args;
  86. {
  87.     unsigned char *cp;
  88.     int len;
  89.  
  90.     if (!PyArg_Parse(args, "s#", &cp, &len))
  91.         return NULL;
  92.  
  93.     MD5Update(&self->md5, cp, len);
  94.  
  95.     Py_INCREF(Py_None);
  96.     return Py_None;
  97. }
  98.  
  99. static PyObject *
  100. md5_digest(self, args)
  101.     md5object *self;
  102.     PyObject *args;
  103. {
  104.  
  105.     MD5_CTX mdContext;
  106.     unsigned char aDigest[16];
  107.  
  108.     if (!PyArg_NoArgs(args))
  109.         return NULL;
  110.  
  111.     /* make a temporary copy, and perform the final */
  112.     mdContext = self->md5;
  113.     MD5Final(aDigest, &mdContext);
  114.  
  115.     return PyString_FromStringAndSize((char *)aDigest, 16);
  116. }
  117.  
  118. static PyObject *
  119. md5_copy(self, args)
  120.     md5object *self;
  121.     PyObject *args;
  122. {
  123.     md5object *md5p;
  124.  
  125.     if (!PyArg_NoArgs(args))
  126.         return NULL;
  127.  
  128.     if ((md5p = newmd5object()) == NULL)
  129.         return NULL;
  130.  
  131.     md5p->md5 = self->md5;
  132.  
  133.     return (PyObject *)md5p;
  134. }
  135.  
  136. static PyMethodDef md5_methods[] = {
  137.     {"update",        (PyCFunction)md5_update},
  138.     {"digest",        (PyCFunction)md5_digest},
  139.     {"copy",        (PyCFunction)md5_copy},
  140.     {NULL,            NULL}        /* sentinel */
  141. };
  142.  
  143. static PyObject *
  144. md5_getattr(self, name)
  145.     md5object *self;
  146.     char *name;
  147. {
  148.     return Py_FindMethod(md5_methods, (PyObject *)self, name);
  149. }
  150.  
  151. statichere PyTypeObject MD5type = {
  152.     PyObject_HEAD_INIT(&PyType_Type)
  153.     0,              /*ob_size*/
  154.     "md5",              /*tp_name*/
  155.     sizeof(md5object),      /*tp_size*/
  156.     0,              /*tp_itemsize*/
  157.     /* methods */
  158.     (destructor)md5_dealloc,  /*tp_dealloc*/
  159.     0,              /*tp_print*/
  160.     (getattrfunc)md5_getattr, /*tp_getattr*/
  161.     0,              /*tp_setattr*/
  162.     0,              /*tp_compare*/
  163.     0,              /*tp_repr*/
  164.         0,              /*tp_as_number*/
  165. };
  166.  
  167.  
  168. /* MD5 functions */
  169.  
  170. static PyObject *
  171. MD5_new(self, args)
  172.     PyObject *self;
  173.     PyObject *args;
  174. {
  175.     md5object *md5p;
  176.     unsigned char *cp = NULL;
  177.     int len = 0;
  178.  
  179.     if (!PyArg_ParseTuple(args, "|s#", &cp, &len))
  180.         return NULL;
  181.  
  182.     if ((md5p = newmd5object()) == NULL)
  183.         return NULL;
  184.  
  185.     if (cp)
  186.         MD5Update(&md5p->md5, cp, len);
  187.  
  188.     return (PyObject *)md5p;
  189. }
  190.  
  191.  
  192. /* List of functions exported by this module */
  193.  
  194. static PyMethodDef md5_functions[] = {
  195.     {"new",        (PyCFunction)MD5_new, 1},
  196.     {"md5",        (PyCFunction)MD5_new, 1}, /* Backward compatibility */
  197.     {NULL,        NULL}    /* Sentinel */
  198. };
  199.  
  200.  
  201. /* Initialize this module. */
  202.  
  203. void
  204. initmd5()
  205. {
  206.     (void)Py_InitModule("md5", md5_functions);
  207. }
  208.